package redis

import (
	"cart_srv/cart"
	"context"
	"fmt"
	"strconv"
)

func CartAdd(cart map[string]interface{}) error {
	key := fmt.Sprintf("cart:user_id_%d:good_id_%d", cart["user_id"], cart["good_id"])
	return Rdb.HMSet(context.Background(), key, cart).Err()
}

func GetExists(userId, goodId int) bool {
	key := fmt.Sprintf("cart:user_id_%d:good_id_%d", userId, goodId)
	return Rdb.Exists(context.Background(), key).Val() == 1
}

func UpdateNum(userId, goodId, num int) error {
	key := fmt.Sprintf("cart:user_id_%d:good_id_%d", userId, goodId)
	return Rdb.HIncrBy(context.Background(), key, "num", int64(num)).Err()
}

func CartDel(userId, goodId int) {
	key := fmt.Sprintf("cart:user_id_%d:good_id_%d", userId, goodId)
	Rdb.Del(context.Background(), key)
}

func CartShow(userId, goodId int) (Show []*cart.ListItems, err error) {
	key := fmt.Sprintf("cart:user_id_%d:good_id_%d", userId, goodId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, s := range keys {
		mapShow := Rdb.HGetAll(context.Background(), s).Val()
		UserId, _ := strconv.Atoi(mapShow["user_id"])
		goodId, _ := strconv.Atoi(mapShow["good_id"])
		goodstock, _ := strconv.Atoi(mapShow["good_stock"])
		goodprice, _ := strconv.ParseFloat(mapShow["good_price"], 64)
		checked, _ := strconv.Atoi(mapShow["checked"])

		Show = append(Show, &cart.ListItems{
			UserId:     int64(UserId),
			GoodNumber: mapShow["good_number"],
			GoodTitle:  mapShow["good_title"],
			GoodStock:  int64(goodstock),
			GoodPrice:  float32(goodprice),
			Checked:    int64(checked),
			Id:         int64(goodId),
		})
	}
	return
}

func CartCheck(userId, checked int) {
	key := fmt.Sprintf("cart:user_id_%d:good_id_*", userId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, s := range keys {
		Rdb.HSet(context.Background(), s, "checked", checked)
	}
}

func CartTotal(userId int) (count int, prices float32) {
	key := fmt.Sprintf("cart:user_id_%d:good_id_*", userId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, s := range keys {
		mapShow := Rdb.HGetAll(context.Background(), s).Val()
		num, _ := strconv.Atoi(mapShow["num"])
		goodprice, _ := strconv.ParseFloat(mapShow["good_price"], 64)
		checked, _ := strconv.Atoi(mapShow["checked"])

		if checked == 1 {
			count += num
			prices = float32(num * int(goodprice))
		}
	}
	return
}
func CartList(userId int) (Show []*cart.ListItem, err error) {
	key := fmt.Sprintf("cart:user_id_%d:good_id_*", userId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, s := range keys {
		mapShow := Rdb.HGetAll(context.Background(), s).Val()
		UserId, _ := strconv.Atoi(mapShow["user_id"])
		goodId, _ := strconv.Atoi(mapShow["good_id"])
		goodstock, _ := strconv.Atoi(mapShow["good_stock"])
		goodprice, _ := strconv.ParseFloat(mapShow["good_price"], 64)
		checked, _ := strconv.Atoi(mapShow["checked"])

		Show = append(Show, &cart.ListItem{
			UserId:     int64(UserId),
			GoodNumber: mapShow["good_number"],
			GoodTitle:  mapShow["good_title"],
			GoodStock:  int64(goodstock),
			GoodPrice:  float32(goodprice),
			Checked:    int64(checked),
			Id:         int64(goodId),
		})
	}
	return
}
func CartClear(userId int) {
	key := fmt.Sprintf("cart:user_id_%d:good_id_*", userId)
	keys := Rdb.Keys(context.Background(), key).Val()
	for _, s := range keys {
		Rdb.Del(context.Background(), s)
	}
}

func CartChecks(userId, goodId, checked int) {
	key := fmt.Sprintf("cart:user_id_%d:good_id_%d", userId, goodId)
	Rdb.HSet(context.Background(), key, "checked", checked)
}
